package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    void insert(Orders orders);

    /**
     * 查询该用户所有订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @Select("select * from orders where user_id=#{userId} order by order_time desc")
    List<Orders> list(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders selectById(Long id);

    /**
     * 取消订单
     *
     * @param orders
     */
    void updateStatus(Orders orders);

    /**
     * 条件查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 查询不同状态的订单数量
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status=#{status}")
    Integer selectCountByStatus(int status);

    /**
     * 订单更新
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据状态、订单查询未支付超时订单
     * @param status
     * @param dateTime
     * @return
     */
    List<Orders> getByStatusAndTime(Integer status, LocalDateTime dateTime);

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 根据订单号查询当前用户的订单
     * @param outTradeNo
     * @param userId
     * @return
     */
    @Select("select * from orders where number=#{outTradeNo} and user_id=#{userId}")
    Orders getByNumberAndUserId(String outTradeNo, Long userId);

    /**
     * 营业额数据统计
     * @param map
     * @return
     */
    Double sumForMap(Map map);

    /**
     * 订单数据统计
     * @param map
     * @return
     */
    Integer getCountByMap(Map map);

    /**
     * 销量排名统计
     * @param beginTime
     * @param endTime
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime beginTime, LocalDateTime endTime);
}
